/**
 * Copyright (c) qc&Bull Co.,LTD.
 * All right reserved.
 */

package cn.zxd.lis.schema;

import cn.zxd.lis.db.LDMaxNoDB;
import cn.zxd.util.*;


import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * <p>ClassName: LDMaxNoSchema </p>
 * <p>Description: DB层 Schema 类文件 </p>
 * <p>Company: qc&Bull Co.,LTD </p>
 * @Database: IIEP_Data_Model
 * @author：anonymous
 * @CreateDate：2015-11-22
 */
public class LDMaxNoSchema implements Schema, Cloneable
{
    // @Field
    /** 号码类型 */
    private String NoType;
    /** 号码限制条件 */
    private String NoLimit;
    /** 当前最大值 */
    private int MaxNo;

    /** 数据库表的字段个数 */
    public static final int FIELDNUM = 3;

    /** 主键 */
    private static String[] PK;

    /** 错误信息 */
    public CErrors mErrors;

    // @Constructor
    public LDMaxNoSchema()
    {
        mErrors = new CErrors();
        String[] pk = new String[2];
        pk[0] = "NoType";
        pk[1] = "NoLimit";
        PK = pk;
    }

    /**
     * Schema克隆
     * @return Object
     * @throws CloneNotSupportedException
     */
    public Object clone() throws CloneNotSupportedException
    {
        LDMaxNoSchema cloned = (LDMaxNoSchema)super.clone();
        cloned.mErrors = (CErrors) mErrors.clone();
        return cloned;
    }

    // @Method
    public String[] getPK()
    {
        return PK;
    }

    public String getNoType()
    {
        return NoType;
    }

    public void setNoType(String aNoType)
    {
        NoType = StrTool.cTrim(aNoType);
    }

    public String getNoLimit()
    {
        return NoLimit;
    }

    public void setNoLimit(String aNoLimit)
    {
        NoLimit = StrTool.cTrim(aNoLimit);
    }

    public int getMaxNo()
    {
        return MaxNo;
    }

    public void setMaxNo(int aMaxNo)
    {
        MaxNo = aMaxNo;
    }

    public void setMaxNo(String aMaxNo)
    {
        if (aMaxNo != null && !aMaxNo.equals(""))
        {
            Integer tInteger = new Integer(aMaxNo);
            int i = tInteger.intValue();
            MaxNo = i;
        }
    }


    /**
     * 使用另外一个 LDMaxNoSchema 对象给 Schema 赋值
     * @param: cLDMaxNoSchema LDMaxNoSchema
     */
    public void setSchema(LDMaxNoSchema cLDMaxNoSchema)
    {
        this.NoType = cLDMaxNoSchema.getNoType();
        this.NoLimit = cLDMaxNoSchema.getNoLimit();
        this.MaxNo = cLDMaxNoSchema.getMaxNo();
    }

    /**
     * 使用 ResultSet 中的第 i 行给 Schema 赋值
     * @param: rs ResultSet
     * @param: i int
     * @return: boolean
     */
    public boolean setSchema(ResultSet rs, int i)
    {
        try
        {
            if (rs.getString(1) == null)
                this.NoType = null;
            else
                this.NoType = rs.getString(1);
            if (rs.getString(2) == null)
                this.NoLimit = null;
            else
                this.NoLimit = rs.getString(2);
            this.MaxNo = rs.getInt(3);
        }
        catch (SQLException sqle)
        {
            sqle.printStackTrace();
            // @@错误处理
            CError tError = new CError();
            tError.moduleName = "LDMaxNoSchema";
            tError.functionName = "setSchema";
            tError.errorMessage = sqle.toString();
            this.mErrors.addOneError(tError);
            return false;
        }
        return true;
    }

    public LDMaxNoSchema getSchema()
    {
        LDMaxNoSchema aLDMaxNoSchema = new LDMaxNoSchema();
        aLDMaxNoSchema.setSchema(this);
        return aLDMaxNoSchema;
    }

    public LDMaxNoDB getDB()
    {
        LDMaxNoDB aDBOper = new LDMaxNoDB();
        aDBOper.setSchema(this);
        return aDBOper;
    }

    /**
     * 数据打包，按 XML 格式打包
     * @return: String 返回打包后字符串
     */
    public String encode()
    {
        StringBuffer strReturn = new StringBuffer(256);
        strReturn.append(StrTool.cTrim(NoType));
        strReturn.append(SysConst.PACKAGESPILTER);
        strReturn.append(StrTool.cTrim(NoLimit));
        strReturn.append(SysConst.PACKAGESPILTER);
        strReturn.append(ChgData.chgData(MaxNo));
        return strReturn.toString();
    }

    /**
     * 数据解包
     * @param: strMessage String 包含一条纪录数据的字符串
     * @return: boolean
     */
    public boolean decode(String strMessage)
    {
        try
        {
            NoType = StrTool.getStr(strMessage, 1, SysConst.PACKAGESPILTER);
            NoLimit = StrTool.getStr(strMessage, 2, SysConst.PACKAGESPILTER);
            MaxNo = new Integer(ChgData.chgNumericStr(StrTool.getStr(strMessage, 3, SysConst.PACKAGESPILTER))).intValue();
        }
        catch (NumberFormatException ex)
        {
            // @@错误处理
            CError tError = new CError();
            tError.moduleName = "LDMaxNoSchema";
            tError.functionName = "decode";
            tError.errorMessage = ex.toString();
            this.mErrors.addOneError(tError);
            return false;
        }
        return true;
    }

    /**
     * 取得对应传入参数的String形式的字段值
     * @param: FCode String 希望取得的字段名
     * @return: String
     * 如果没有对应的字段，返回""
     * 如果字段值为空，返回"null"
     */
    public String getV(String FCode)
    {
        String strReturn = "";
        if (FCode.equalsIgnoreCase("NoType"))
        {
            strReturn = StrTool.cTrim(NoType);
        }
        if (FCode.equalsIgnoreCase("NoLimit"))
        {
            strReturn = StrTool.cTrim(NoLimit);
        }
        if (FCode.equalsIgnoreCase("MaxNo"))
        {
            strReturn = String.valueOf(MaxNo);
        }
        if (strReturn.equals(""))
        {
            strReturn = "null";
        }
        return strReturn;
    }

    /**
     * 取得Schema中指定索引值所对应的字段值
     * @param: nFieldIndex int 指定的字段索引值
     * @return: String
     * 如果没有对应的字段，返回""
     * 如果字段值为空，返回"null"
     */
    public String getV(int nFieldIndex)
    {
        String strFieldValue = "";
        switch (nFieldIndex)
        {
            case 0:
                strFieldValue = StrTool.cTrim(NoType);
                break;
            case 1:
                strFieldValue = StrTool.cTrim(NoLimit);
                break;
            case 2:
                strFieldValue = String.valueOf(MaxNo);
                break;
            default:
                strFieldValue = "";
        }
        if (strFieldValue.equals(""))
        {
            strFieldValue = "null";
        }
        return strFieldValue;
    }

    /**
     * 设置对应传入参数的String形式的字段值
     * @param: FCode String 需要赋值的对象
     * @param: FValue String 要赋的值
     * @return: boolean
     */
    public boolean setV(String FCode, String FValue)
    {
        if (StrTool.cTrim(FCode).equals(""))
            return false;

        if (FCode.equalsIgnoreCase("NoType"))
        {
            if (FValue != null)
            {
                NoType = FValue.trim();
            }
            else
                NoType = null;
        }
        if (FCode.equalsIgnoreCase("NoLimit"))
        {
            if (FValue != null)
            {
                NoLimit = FValue.trim();
            }
            else
                NoLimit = null;
        }
        if (FCode.equalsIgnoreCase("MaxNo"))
        {
            if (FValue != null && !FValue.equals(""))
            {
                Integer tInteger = new Integer(FValue);
                int i = tInteger.intValue();
                MaxNo = i;
            }
        }
        return true;
    }

    public boolean equals(Object otherObject)
    {
        if (this == otherObject)
            return true;
        if (otherObject == null)
            return false;
        if (getClass() != otherObject.getClass())
            return false;
        LDMaxNoSchema other = (LDMaxNoSchema) otherObject;
        return
                (NoType == null ? other.getNoType() == null : NoType.equals(other.getNoType()))
                        && (NoLimit == null ? other.getNoLimit() == null : NoLimit.equals(other.getNoLimit()))
                        && MaxNo == other.getMaxNo();
    }

    /**
     * 取得Schema拥有字段的数量
     * @return: int
     */
    public int getFieldCount()
    {
        return FIELDNUM;
    }

    /**
     * 取得Schema中指定字段名所对应的索引值
     * 如果没有对应的字段，返回-1
     * @param: strFieldName String
     * @return: int
     */
    public int getFieldIndex(String strFieldName)
    {
        if (strFieldName.equals("NoType"))
        {
            return 0;
        }
        if (strFieldName.equals("NoLimit"))
        {
            return 1;
        }
        if (strFieldName.equals("MaxNo"))
        {
            return 2;
        }
        return -1;
    }

    /**
     * 取得Schema中指定索引值所对应的字段名
     * 如果没有对应的字段，返回""
     * @param: nFieldIndex int
     * @return: String
     */
    public String getFieldName(int nFieldIndex)
    {
        String strFieldName = "";
        switch (nFieldIndex)
        {
            case 0:
                strFieldName = "NoType";
                break;
            case 1:
                strFieldName = "NoLimit";
                break;
            case 2:
                strFieldName = "MaxNo";
                break;
            default:
                strFieldName = "";
        }
        return strFieldName;
    }

    /**
     * 取得Schema中指定字段名所对应的字段类型
     * 如果没有对应的字段，返回Schema.TYPE_NOFOUND
     * @param: strFieldName String
     * @return: int
     */
    public int getFieldType(String strFieldName)
    {
        if (strFieldName.equals("NoType"))
        {
            return Schema.TYPE_STRING;
        }
        if (strFieldName.equals("NoLimit"))
        {
            return Schema.TYPE_STRING;
        }
        if (strFieldName.equals("MaxNo"))
        {
            return Schema.TYPE_INT;
        }
        return Schema.TYPE_NOFOUND;
    }

    /**
     * 取得Schema中指定索引值所对应的字段类型
     * 如果没有对应的字段，返回Schema.TYPE_NOFOUND
     * @param: nFieldIndex int
     * @return: int
     */
    public int getFieldType(int nFieldIndex)
    {
        int nFieldType = Schema.TYPE_NOFOUND;
        switch (nFieldIndex)
        {
            case 0:
                nFieldType = Schema.TYPE_STRING;
                break;
            case 1:
                nFieldType = Schema.TYPE_STRING;
                break;
            case 2:
                nFieldType = Schema.TYPE_INT;
                break;
            default:
                nFieldType = Schema.TYPE_NOFOUND;
        }
        return nFieldType;
    }
}
